package com.ohuang.jiaxiaotong.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ohuang.jiaxiaotong.entity.CourseImg;

import java.util.ArrayList;
import com.ohuang.jiaxiaotong.R;

public class CourseRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private ArrayList<CourseImg> mData;

    public CourseRvAdapter(Context mContext, ArrayList<CourseImg> mData) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CourseRvAdapter.MyCourseHolder(mLayoutInflater.inflate(R.layout.rv_course_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CourseRvAdapter.MyCourseHolder) {
            ((MyCourseHolder) holder).mTextView.setText(mData.get(position).getName());
            Glide.with(mContext).load(mData.get(position).getPath()).into(((MyCourseHolder) holder).mImageView);
        }

        if (onItemClickListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyCourseHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;

        MyCourseHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.txt_rv_course);
            mImageView = itemView.findViewById(R.id.img_rv_course);
        }
    }

    private CourseRvAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder holder, int position);
    }

}
